//#preprocess
package org.lilred.nzbberry.controllers;
import net.rim.device.api.ui.ContextMenu;
import net.rim.device.api.ui.MenuItem;
import org.lilred.nzbberry.models.QueueNzbModel;
import org.lilred.nzbberry.views.QueueSlotView;

public class QueueSlotController {
	QueueSlotView view;
	public QueueNzbModel model;
	QueueController queueController;

	public QueueSlotController(QueueController q, QueueNzbModel m) {
		this.queueController = q;
		this.model = m;
		this.view = new QueueSlotView(model) {
			protected void makeContextMenu(ContextMenu contextMenu) {
				if (model.isPaused()) contextMenu.addItem(resumeMenuItem);
				else contextMenu.addItem(pauseMenuItem);	
				if (model.getIndex() > 0) contextMenu.addItem(moveUpMenuItem);
				if (model.getIndex() < queueController.size() - 1) contextMenu.addItem(moveDownMenuItem);
				contextMenu.addItem(deleteMenuItem);
			}

			//#ifndef BlackBerry4.7
			/*
			//#endif
			protected boolean touchEvent(net.rim.device.api.ui.TouchEvent message) {
				int eventCode = message.getEvent();
				if (eventCode == net.rim.device.api.ui.TouchEvent.GESTURE) {
					net.rim.device.api.ui.TouchGesture gesture = message.getGesture();
					int gestureCode = gesture.getEvent();
					if (gestureCode == net.rim.device.api.ui.TouchGesture.SWIPE) {
						if (gesture.getSwipeDirection() == net.rim.device.api.ui.TouchGesture.SWIPE_EAST) {
							moveDown();
							return true;
						}
						if (gesture.getSwipeDirection() == net.rim.device.api.ui.TouchGesture.SWIPE_WEST) {
							moveUp(); 
							return true;
						}
					}
				}
				return false;
			}
			//#ifndef BlackBerry4.7
			*/
			//#endif
		};
	}

	public void setModel(QueueNzbModel model) {
		this.model = model;
		view.setModel(model);
	}

	public int getIndex() {
		return model.getIndex();
	}

	public void setIndex(int index) {
		model.setIndex(index);
	}

	MenuItem pauseMenuItem = new MenuItem("Pause", 30, 30) {
		public void run() {
			pause();
		}
	};

	MenuItem resumeMenuItem = new MenuItem("Unpause", 40, 40) {
		public void run() {
			resume();
		}
	};

	MenuItem moveUpMenuItem = new MenuItem("Move Up", 10, 10) {
		public void run() {
			moveUp();
		}
	};

	MenuItem moveDownMenuItem = new MenuItem("Move Down", 20, 20) {
		public void run() {
			moveDown();
		}
	};

	MenuItem deleteMenuItem = new MenuItem("Delete", 50, 50) {
		public void run() {
			delete();
		}
	};

	protected void pause() {
		model.pause();
		view.invalidate();
	}

	protected void moveDown() {
		if (model.getIndex() < queueController.size() - 1) {
			model.moveDown();
			queueController.reorder();
		}
	}

	protected void moveUp() {
		if (model.getIndex() != 0){
			model.moveUp();
			queueController.reorder();
		}
	}

	protected void resume() {
		model.resume();
		view.invalidate();
	}

	protected void delete() {
		model.delete();
		queueController.delete(this);
		//view.invalidate();
	}
}
